package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class Test4 {
    public static void main(String[] args) throws Exception {
        File file = new File(
                Test4.class.getResource(".").toURI()
        );

        File[] subs = file.listFiles(f -> f.getName().endsWith(".class"));
        for (File sub: subs){
            String packageName = Test4.class.getPackage().getName();
            String className = sub.getName().replace(".class", "");
            Class cls = Class.forName(packageName+"." + className);
            if (cls.isAnnotationPresent(AutoRunClass.class) ){
                Object obj = cls.newInstance();
                Method[] methods = cls.getDeclaredMethods();
                for (Method method:methods){
                    if (method.isAnnotationPresent(AutoRunMethod.class)){
                        method.invoke(obj);
                    }
                }
            }
        }
    }
}
